<form class="layui-form" lay-filter="add-form" action="{:url('', ['material_sku' => $materialSku, 'client_ordernum' => $clientOrdernum, 'material_return_id' => $materialReturnId])}" method="post" id="editForm">
    
    {:FormExt::input('物料SKU', 'material_sku', arrayGet($materialInfo, 'material_sku', ''), '请输入物料SKU', 'required', 'lay-verify="required"')}
    {:FormExt::input('订单号', 'client_ordernum', $clientOrdernum, '请输入订单号', '', '')}
    {:FormExt::select('工厂', 'factory_id', arrayGet($orderInfo, 'factory_id', 0), $factoryKV, '', '')}
    {:FormExt::input('米数', 'metre', '', '请输入米数', '', 'lay-verify="number"')}
    {:FormExt::input('重量', 'weight', '', '请输入重量', '', 'lay-verify=""')}
    {:FormExt::radio('计费方式', 'amount_type', $amountType, ['1' => '计重', '2' => '计米'], '', 'lay-filter="amount-type"')}
    {:FormExt::input('单价', 'unit_price', '', '请输入单价', '', 'lay-verify="number"')}
    {:FormExt::input('总金额', 'amount', '', '请输入总金额', '', 'lay-verify="number"')}
    {:FormExt::select('货仓', 'storehouse_id', '', $storehouseKV, '', '', 'lay-filter="storehouse-id"')}
    {:FormExt::select('货架', 'storeshelf_id', '', [], '', '')}
    <div class="pop-bottom-bar">
    	<input type="hidden" class="field-id" name="id">
        <button type="submit" class="layui-btn layui-btn-normal" lay-submit="" lay-filter="formSubmit" hisi-data="{pop: true, refresh: false, callback: callbackUpdateStock}">提交保存</button>
        <a href="javascript:parent.layui.layer.closeAll();" class="layui-btn layui-btn-primary ml10">取消</a>
    </div>
</form>

{include file="system@block/layui" /}
    
<script type="text/javascript">
	var weightWidth = {:$weightWidth};
	var storeshelfMap = {:json_encode($storeshelfMap)};
    layui.use(['tool', 'form'], function() {
    	var $ = layui.jquery;
    	var form = layui.form;
    	$('input[name=metre]').focus();
    	
    	window.calAmount = function(){
    		var d = form.val("add-form");
    		var reg = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
    		
    		if (d.weight != '') {
    			if (!reg.test(d.weight)) {
    				layer.msg('重量格式错误', {icon: 2,anim: 6});
    				return false;
    			}
    			$("input[name=metre]").val(layui.tool.bcmul(d.weight, weightWidth));
    		}

    		if (d.amount_type == 1) {
    			if (d.weight != '') {
    				if (!reg.test(d.weight)) {
        				layer.msg('重量格式错误', {icon: 2,anim: 6});
        				return false;
        			}
    				$("input[name=amount]").val(layui.tool.bcmul(d.weight, d.unit_price));
    			}
    		} else if (d.amount_type == 2) {
    			if (d.metre != '') {
    				if (!reg.test(d.metre)) {
        				layer.msg('米数格式错误', {icon: 2,anim: 6});
        				return false;
        			}
    			}
    			if (d.unit_price != '') {
    				if (!reg.test(d.unit_price)) {
        				layer.msg('单价格式错误', {icon: 2,anim: 6});
        				return false;
        			}
    				$("input[name=amount]").val(layui.tool.bcmul(d.metre, d.unit_price));
    			}
    			if (d.metre != '' && d.unit_price != '') {
    				$("input[name=amount]").val(layui.tool.bcmul(d.metre, d.unit_price));
    			}
    		}
    	};
    	
    	form.on('radio(amount-type)', function(data){
    		calAmount();
    	});  
    	
    	$(document).on('blur','input[name=metre],input[name=weight],input[name=unit_price]',function(){
    		calAmount();
    	});
    	
    	// 表单提交回调
    	window.callbackUpdateStock = function(obj, res) {
    		var stock = 0;
    		var mid = {$mid};
    		if (mid > 0){
    			// 更新采购进度库存数
    			if (typeof(eval(parent.window.updateStock))==='function'){
    				parent.window.updateStock(mid, res.data.stock);
    			}
    		}
    		
    		// 更新退料进度状态
    		if (typeof(eval(parent.window.updateMaterialReturnStatus))==='function'){
    			parent.window.updateMaterialReturnStatus(res.data.material_return_id);
    		}
    		
    		setTimeout(function(){
    			parent.layui.layer.closeAll();
    		}, 2000);
    	}
    	
    	// 货仓下拉切换
    	form.on('select(storehouse-id)', function(data){
    		var optionArr = storeshelfMap[data.value];
    		var option = '';
    		for(var i in optionArr) {
    			option += '<option value="'+i+'">'+optionArr[i]+'</option>';
    		}
    		$("select[name=storeshelf_id] option").remove();
    		$('select[name=storeshelf_id]').append(option);
    		form.render();
    	});
    });
</script>